<template>
  <div class="login-container">
    <div class="login-box">
      <!--头像-->
      <div class="avatar-box">
        <img src="../assets/logo.png">
      </div>
      <!--表单-->
      <el-form class="login-form"
               ref="loginFormRef"
               :model="loginForm"
               :rules="rules"
               @keydown.enter.native="login">
        <el-form-item prop="phoneNumber">
          <div class="input_title">用户名1：</div>
          <el-input v-model="loginForm.phoneNumber"
                    prefix-icon="el-icon-user-solid"
                    placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <div class="input_title">密码：</div>
          <el-input v-model="loginForm.password"
                    type="password"
                    prefix-icon="el-icon-lock"
                    placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <div class="input_title">角色：</div>
          <el-select :value="nowRole" @change="changeRole" placeholder="请选择登录角色">
            <el-option
                v-for="item in roleOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="btns">
          <!--          <div><span >找回密码</span></div>-->
          <el-button type="primary" @click="login" class="login-button">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

import {loginRules} from '@/utils/verify/rules'
import {login} from '@/network/login'
import {SET_ROLE} from "@/store/mutations-types"
import {mapActions, mapState,mapMutations} from 'vuex'

const {title} = require("../setting")
export default {
  name: "Login",
  data() {
    return {
      title,
      // 登录表单数据
      loginForm: {
        phoneNumber: "19840472153",
        password: "1234"
      },
      rules: loginRules
    }
  },
  computed: {
    ...mapState(['nowRole','roleOptions'])
  },
  methods: {
    ...mapActions({"userLogin": "login"}),
    changeRole(roleValue) {
      this.$store.commit(SET_ROLE,roleValue)
    },
    login() {
      // 表单登录前的预校验
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return null
        const result = await this.userLogin(this.loginForm)
        if (result) {
          this.$successMessage("登录成功")
          await this.$router.push('/home');
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-box {
  width: 400px;
  height: 450px;
  backdrop-filter: blur(10px);
  border-radius: 10px;
  box-shadow: 0 0 10px #545454;
  transition: 1s;
}

.avatar-box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 5px;
  box-shadow: 0 0 10px #ddd;
  margin: 0 auto;
  transform: translateY(-50%);
  user-select: none;
  opacity: .9;
}

.avatar-box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.login-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  /*display: flex;*/
  /*justify-content: flex-end;*/
}

.login-button {
  width: 100%;
}

.input_title {
  font-size: 14px;
  font-weight: bold;
  color: #fff;
}

</style>
